//
//  HJMinePresent.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJMinePresent.h"

@implementation HJMinePresent

+ (void)loginWithAccount:(NSString *)account password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:account forKey:@"email"];
    [parameters setValue:password forKey:@"password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_login parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSDictionary *userInfo = data[@"user_info"];
        
        HJUserInfo.isLogin = YES;
        HJUserInfo.email = account;
        HJUserInfo.userId = toString(userInfo[@"id"]);
        HJUserInfo.no = toString(userInfo[@"no"]);
        HJUserInfo.token = data[@"access_token"];
        HJUserInfo.avatar = userInfo[@"profile_picture"];
        HJUserInfo.nickname = userInfo[@"nickname"];
        HJUserInfo.gender = toString(userInfo[@"gender"]);
        HJUserInfo.isCanModify = [data[@"is_can_modify_no"] boolValue];

        [[NSNotificationCenter defaultCenter] postNotificationName:loginSucceedNotification object:nil];
        !complete ?: complete(nil);

    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)registerWithAccount:(NSString *)account password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:account forKey:@"email"];
    [parameters setValue:password forKey:@"set_password"];
    [parameters setValue:password forKey:@"confirm_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_register parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [[NSNotificationCenter defaultCenter] postNotificationName:registerSucceedNotification object:nil];
        !complete ?: complete(data);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)sendRegisterEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"register" forKey:@"type"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_sendEmail parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)sendResetPasswordEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"reset" forKey:@"type"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_sendEmail parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)verifyRegisterCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"register" forKey:@"type"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerify parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)verifyResetPasswordCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"reset" forKey:@"type"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerify parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)verifyWalletOperationCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"wallet" forKey:@"type"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerify parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
        
        
    }];
}

+ (void)setPasswordWithEmail:(NSString *)email password:(NSString *)password  complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:password forKey:@"set_password"];
    [parameters setValue:password forKey:@"confirm_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_setPassword parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [[NSNotificationCenter defaultCenter] postNotificationName:registerSucceedNotification object:nil];
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)refreshTokenComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_refreshToken parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)logoutComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_logOut parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)revokedComplete:(void (^__nullable)(NSError * _Nullable error, NSString *message))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_revoked parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        HJUserInfo.email = nil;
        [HJUserInfo clear];
        [[NSNotificationCenter defaultCenter] postNotificationName:cancellationSucceedNotification object:nil];
        !complete ?: complete(nil, responseObject[@"msg"]);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error, nil);
    }];
}

+ (void)unfreezeWithEmail:(NSString *)email password:(NSString *)password  complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:password forKey:@"password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_unfreeze parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)getAgreementComplete:(void (^__nullable)(NSString * _Nullable url))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_user_agree parameters:@{
        @"agree_name": @"user_agreement"
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSString *agreement = data[@"user_agreement"];
        HJUserInfo.agreement = agreement;
        !complete ?: complete(agreement);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)loadVersionInfoComplete:(void (^__nullable)(NSDictionary * _Nullable data, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_versionInfo parameters:@{
        @"type": @"2",
        @"channel": @"appstore"
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSDictionary *app_version = data[@"app_version"];
        if ([app_version isKindOfClass:NSDictionary.class]) {
            !complete ?: complete(app_version, nil);
        } else {
            !complete ?: complete(nil,[NSError errorWithCode:-1001 errorReason:@"获取版本号失败"]);
        }
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}
 
+ (void)loadVersionListWithPageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize complete:(void (^__nullable)(NSArray * _Nullable data, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_versionList parameters:@{
        @"type": @"2",
        @"channel": @"appstore",
        @"page_no": @(pageIndex),
        @"page_size": @(pageSize)
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"list"], nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)loadUserInfoComplete:(void (^__nullable)(NSDictionary * _Nullable info))complete {
    [HJNetworking.shareNetwork GET:API_selfInfo parameters:@{
        @"agree_name": @"user_agreement"
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        
        NSDictionary *userInfo = data[@"user_info"];

        HJUserInfo.userId = toString(userInfo[@"id"]);
        HJUserInfo.no = toString(userInfo[@"no"]);
        HJUserInfo.avatar = userInfo[@"profile_picture"];
        HJUserInfo.nickname = userInfo[@"nickname"];
        HJUserInfo.gender = toString(userInfo[@"gender"]);
        HJUserInfo.isCanModify = [data[@"is_can_modify_no"] boolValue];

        !complete ?: complete(userInfo);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

@end
